/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.netbrain.dao.impl;

import java.util.List;
import mx.netbrain.dao.OrganizationDao;
import mx.netbrain.model.Organization;
import mx.netbrain.util.HibernateUtil;
import org.hibernate.Session;

/**
 *
 * @author gamma
 */
public class OrganizationDaoImpl implements OrganizationDao {

    @Override
    public void save(Organization organization) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.persist(organization);
            session.beginTransaction().commit();
        } catch (Exception e) {
            System.out.println("Error al guardar la empresa ===>" + e.getMessage());
            session.beginTransaction().rollback();
        }
    }

    @Override
    public void update(Organization organization) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.merge(organization);
            session.beginTransaction().commit();
        } catch (Exception e) {
            System.out.println("Error al actualizar la empresa");
            session.beginTransaction().rollback();
        }
    }

    @Override
    public Organization findById(Long id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return (Organization) session.load(Organization.class, id);
    }

    @Override
    public List<Organization> listAll() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return session.createQuery("from Organization").list();
    }

    @Override
    public void delete(Organization organization) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.delete(organization);
            session.beginTransaction().commit();
        } catch (Exception e) {
            System.out.println("Error al eliminar el registro de la empresa ===>" + e.getMessage());
            session.beginTransaction().rollback();
        }
    }
}
